const express = require('express')
//引入token
const jwt = require('express-jwt')

const cors = require('cors')

const bodyParser = require('body-parser')

const server=express()
// 解决跨域问题
server.use(cors())
// 验证
server.use(jwt({ secret: 'token', algorithms: ['HS256']}).unless({path: ['/login/login','/login/register','/static/uploads']}))

// 解析请求体参数
server.use(express.urlencoded({ extended: true }));

server.use(bodyParser.json())

 

server.use(function (err, req, res, next) {
  next();
})
 

server.use('/login',require('./modules/login'))
server.use('/user',require('./modules/user'))
server.use('/systemInfo',require('./modules/info'))
server.use('/comment',require('./modules/comment'))
server.use('/order',require('./modules/order'))
server.use('/collection',require('./modules/collection'))
server.use('/recommend',require('./modules/recommend'))
server.use('/buy_car',require('./modules/buyCar'))
server.use('/details',require('./modules/details'))
server.use('/history', require('./modules/history'));
server.use('/upload', require('./modules/upload'));

// 必须放到下面不然访问不了图片资源
 // 部署静态资源, 部署之后即可通过域名访问文件
 server.use('/static',express.static('./server/static'));


server.use(function (err, req, res, next) {
  if (err.name === 'UnauthorizedError') {
    res.status(401).send({
      code:0,
      msg:'请重新登录！'
    });
  }
});

server.listen(3000, () => {
  console.log(`服务器启动了， http://localhost:${3000}`)
})

 
